package com.bensonbasement.snorflux.client;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;

import net.miginfocom.swing.MigLayout;

import com.bensonbasement.snorflux.server.SnorfluxServerHandler;

public class ConnectDialog extends JDialog {
	
	private JTextField serverHostnameField;
	private JTextField portField;
	private JTextField playerNameField;
	private JButton connectButton;
	private JButton cancelButton;
	
	private boolean done = false;
	
	public ConnectDialog(JFrame parent) {
		super(parent,"Connect to server",true);
		this.setLayout(new MigLayout());
		
		JLabel serverHostnameLabel = new JLabel("Server IP/hostname: ");
		this.serverHostnameField = new JTextField("127.0.0.1",20);
		JLabel portLabel = new JLabel("Port: ");
		this.portField = new JTextField(Integer.toString(SnorfluxServerHandler.DEFAULT_PORT),5);
		JLabel playerNameLabel = new JLabel("Your name: ");
		this.playerNameField = new JTextField("Player",10);
		this.connectButton = new JButton("Connect");
		this.connectButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				ConnectDialog.this.done = true;
				ConnectDialog.this.setVisible(false);
			}
			
		});
		this.cancelButton = new JButton("Cancel");
		this.cancelButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				ConnectDialog.this.done = false;
				ConnectDialog.this.setVisible(false);
			}
		});
		this.add(serverHostnameLabel);
		this.add(this.serverHostnameField, "wrap");
		this.add(portLabel);
		this.add(this.portField, "wrap");
		this.add(playerNameLabel);
		this.add(this.playerNameField, "wrap");
		this.add(this.connectButton);
		this.add(this.cancelButton);
		this.pack();
	}
	
	public int getPort() {
		return Integer.parseInt(this.portField.getText());
	}
	
	public String getHostname() {
		return this.serverHostnameField.getText();
	}
	
	public String getPlayerName() {
		return this.playerNameField.getText();
	}

	public boolean connectRequested() {
		return done;
	}
	
}
